package com.warm.pump.module.sys.dao.gen;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.TypeCast;
import com.warm.pump.frame.gen.exception.ServiceException;
import com.warm.pump.module.sys.bean.po.gen.SysUserRole;
import com.warm.pump.module.sys.mapper.gen.SysUserRoleMapper;
import com.warm.pump.module.sys.bean.po.gen.SysUserRoleExample;

@Repository
public class SysUserRoleDao {
    // 日志对象
    private transient Log log = LogFactory.getLog(this.getClass());
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    // 增加一个后台SysUserRole表

    public boolean insert(SysUserRole sysUserRole) {
        try {
            sysUserRoleMapper.insert(sysUserRole);
            log.debug("后台SysUserRole表增加成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole表增加失败");
            throw new ServiceException("后台SysUserRole表增加失败", e);
        }
    }
    // 增加一个后台SysUserRole表Selective

    public boolean insertSelective(SysUserRole sysUserRole) {
        try {
            sysUserRoleMapper.insertSelective(sysUserRole);
            log.debug("后台SysUserRole表增加成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole表增加失败");
            throw new ServiceException("后台SysUserRole表增加失败", e);
        }
    }

    // 删除一个后台SysUserRole表

    public boolean deleteByPrimaryKey(Long id) {
        try {
            sysUserRoleMapper.deleteByPrimaryKey(id);
            log.debug("后台SysUserRole表删除成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole表删除失败");
            throw new ServiceException("后台SysUserRole表删除失败", e);
        }
    }
    // 删除一个后台SysUserRole表byMap

    public boolean deleteByMap(Map sysUserRoleMap) {
        try {
            sysUserRoleMapper.deleteByExample(createSysUserRoleExample(sysUserRoleMap, null, null));
            log.debug("后台SysUserRole根据Map删除成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole根据GroupID删除失败");
            throw new ServiceException("后台SysUserRole根据object删除失败", e);
        }
    }
    // 修改一个后台SysUserRole表

    public boolean updateByPrimaryKey(SysUserRole sysUserRole) {
        try {
            sysUserRoleMapper.updateByPrimaryKey(sysUserRole);
            log.debug("后台SysUserRole表修改成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole表修改失败");
            throw new ServiceException("后台SysUserRole表修改失败", e);
        }
    }
    // 修改一个后台SysUserRole表Selective

    public boolean updateByPrimaryKeySelective(SysUserRole sysUserRole) {
        try {
            sysUserRoleMapper.updateByPrimaryKeySelective(sysUserRole);
            log.debug("后台SysUserRole表修改成功");
            return true;
        } catch (Exception e) {
            log.debug("后台SysUserRole表修改失败");
            throw new ServiceException("后台SysUserRole表修改失败", e);
        }
    }
    // 修改一个后台SysUserRole表

    public boolean updateByMap(SysUserRole sysUserRole, Map sysUserRoleMap) {
        try {
            sysUserRoleMapper.updateByExample(sysUserRole, createSysUserRoleExample(sysUserRoleMap, null, null));
            log.debug("后台批量SysUserRole表修改成功");
            return true;
        } catch (Exception e) {
            log.debug("后台批量SysUserRole表修改失败");
            throw new ServiceException("后台SysUserRole表批量修改失败", e);
        }
    }
    // 修改一个后台SysUserRole表Selective

    public boolean updateByMapSelective(SysUserRole sysUserRole, Map sysUserRoleMap) {
        try {
            sysUserRoleMapper.updateByExampleSelective(sysUserRole, createSysUserRoleExample(sysUserRoleMap, null, null));
            log.debug("后台批量SysUserRole表修改成功");
            return true;
        } catch (Exception e) {
            log.debug("后台批量SysUserRole表修改失败");
            throw new ServiceException("后台SysUserRole表批量修改失败", e);
        }
    }

    // 查询一个后台SysUserRole表

    public SysUserRole selectByPrimaryKey(Long id) {
        return sysUserRoleMapper.selectByPrimaryKey(id);
    }

    // 查询一个后台SysUserRole表-根据map

    public SysUserRole selectByMap(Map sysUserRoleMap) {
        List<SysUserRole> list = getListByMap(sysUserRoleMap);
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 查询所有后台SysUserRole表

    public List<SysUserRole> getListByMap(Map sysUserRoleMap) {

        return sysUserRoleMapper.selectByExample(createSysUserRoleExample(sysUserRoleMap, null, null));
    }

    //统计后台SysUserRole表数量

    public int countTotalByMap(Map sysUserRoleMap) {

        return sysUserRoleMapper.countByExample(createSysUserRoleExample(sysUserRoleMap, null, null));
    }


    public List<SysUserRole> getListByMapPage(Map sysUserRoleMap, int limitStart,
                                              int limitOffset) {

        return sysUserRoleMapper.selectByExample(createSysUserRoleExample(sysUserRoleMap, limitStart, limitOffset));
    }


    public PageBean getPageBeanByMap(Map sysUserRoleMap, int pageNo, int pageSize) {
        Integer count = countTotalByMap(sysUserRoleMap);
        PageBean pageBean = new PageBean();
        pageBean.setCount(count);
        pageBean.setPageNo(pageNo);
        pageBean.setPageSize(pageSize);
        int limitStart = pageBean.getStartNo();
        int limitOffset = pageSize;
        pageBean.setData(sysUserRoleMapper.selectByExample(createSysUserRoleExample(sysUserRoleMap, limitStart, limitOffset)));
        return pageBean;
    }

    private SysUserRoleExample createSysUserRoleExample(Map sysUserRoleMap, Integer limitStart, Integer limitOffset) {
        SysUserRoleExample sysUserRoleEx = new SysUserRoleExample();
        SysUserRoleExample.Criteria c = sysUserRoleEx.createCriteria();
        Long id_null = TypeCast.getLong(sysUserRoleMap.get("id_null"));
        Long id_notNull = TypeCast.getLong(sysUserRoleMap.get("id_notNull"));
        Long id = TypeCast.getLong(sysUserRoleMap.get("id"));
        Long id_not = TypeCast.getLong(sysUserRoleMap.get("id_not"));
        Long id_greater = TypeCast.getLong(sysUserRoleMap.get("id_greater"));
        Long id_greaterEqual = TypeCast.getLong(sysUserRoleMap.get("id_greaterEqual"));
        Long id_less = TypeCast.getLong(sysUserRoleMap.get("id_less"));
        Long id_lessEqual = TypeCast.getLong(sysUserRoleMap.get("id_lessEqual"));
        List<Long> id_in = TypeCast.getLongList(sysUserRoleMap.get("id_in"));
        List<Long> id_notIn = TypeCast.getLongList(sysUserRoleMap.get("id_notIn"));
        Long id_between1 = TypeCast.getLong(sysUserRoleMap.get("id_between1"));
        Long id_between2 = TypeCast.getLong(sysUserRoleMap.get("id_between2"));
        Long id_notBetween1 = TypeCast.getLong(sysUserRoleMap.get("id_notBetween1"));
        Long id_notBetween2 = TypeCast.getLong(sysUserRoleMap.get("id_notBetween2"));

        if (id_null != null) {
            c.andIdIsNull();
        }
        if (id_notNull != null) {
            c.andIdIsNotNull();
        }
        if (id != null) {
            c.andIdEqualTo(id);
        }
        if (id_not != null) {
            c.andIdNotEqualTo(id_not);
        }
        if (id_greater != null) {
            c.andIdGreaterThan(id_greater);
        }
        if (id_greaterEqual != null) {
            c.andIdGreaterThanOrEqualTo(id_greaterEqual);
        }
        if (id_less != null) {
            c.andIdLessThan(id_less);
        }
        if (id_lessEqual != null) {
            c.andIdLessThanOrEqualTo(id_lessEqual);
        }
        if (id_in != null) {
            c.andIdIn(id_in);
        }
        if (id_notIn != null) {
            c.andIdNotIn(id_notIn);
        }
        if (id_between1 != null) {
            c.andIdBetween(id_between1, id_between2);
        }
        if (id_notBetween1 != null) {
            c.andIdNotBetween(id_notBetween1, id_notBetween2);
        }
        Long userId_null = TypeCast.getLong(sysUserRoleMap.get("userId_null"));
        Long userId_notNull = TypeCast.getLong(sysUserRoleMap.get("userId_notNull"));
        Long userId = TypeCast.getLong(sysUserRoleMap.get("userId"));
        Long userId_not = TypeCast.getLong(sysUserRoleMap.get("userId_not"));
        Long userId_greater = TypeCast.getLong(sysUserRoleMap.get("userId_greater"));
        Long userId_greaterEqual = TypeCast.getLong(sysUserRoleMap.get("userId_greaterEqual"));
        Long userId_less = TypeCast.getLong(sysUserRoleMap.get("userId_less"));
        Long userId_lessEqual = TypeCast.getLong(sysUserRoleMap.get("userId_lessEqual"));
        List<Long> userId_in = TypeCast.getLongList(sysUserRoleMap.get("userId_in"));
        List<Long> userId_notIn = TypeCast.getLongList(sysUserRoleMap.get("userId_notIn"));
        Long userId_between1 = TypeCast.getLong(sysUserRoleMap.get("userId_between1"));
        Long userId_between2 = TypeCast.getLong(sysUserRoleMap.get("userId_between2"));
        Long userId_notBetween1 = TypeCast.getLong(sysUserRoleMap.get("userId_notBetween1"));
        Long userId_notBetween2 = TypeCast.getLong(sysUserRoleMap.get("userId_notBetween2"));

        if (userId_null != null) {
            c.andUserIdIsNull();
        }
        if (userId_notNull != null) {
            c.andUserIdIsNotNull();
        }
        if (userId != null) {
            c.andUserIdEqualTo(userId);
        }
        if (userId_not != null) {
            c.andUserIdNotEqualTo(userId_not);
        }
        if (userId_greater != null) {
            c.andUserIdGreaterThan(userId_greater);
        }
        if (userId_greaterEqual != null) {
            c.andUserIdGreaterThanOrEqualTo(userId_greaterEqual);
        }
        if (userId_less != null) {
            c.andUserIdLessThan(userId_less);
        }
        if (userId_lessEqual != null) {
            c.andUserIdLessThanOrEqualTo(userId_lessEqual);
        }
        if (userId_in != null) {
            c.andUserIdIn(userId_in);
        }
        if (userId_notIn != null) {
            c.andUserIdNotIn(userId_notIn);
        }
        if (userId_between1 != null) {
            c.andUserIdBetween(userId_between1, userId_between2);
        }
        if (userId_notBetween1 != null) {
            c.andUserIdNotBetween(userId_notBetween1, userId_notBetween2);
        }
        Long roleId_null = TypeCast.getLong(sysUserRoleMap.get("roleId_null"));
        Long roleId_notNull = TypeCast.getLong(sysUserRoleMap.get("roleId_notNull"));
        Long roleId = TypeCast.getLong(sysUserRoleMap.get("roleId"));
        Long roleId_not = TypeCast.getLong(sysUserRoleMap.get("roleId_not"));
        Long roleId_greater = TypeCast.getLong(sysUserRoleMap.get("roleId_greater"));
        Long roleId_greaterEqual = TypeCast.getLong(sysUserRoleMap.get("roleId_greaterEqual"));
        Long roleId_less = TypeCast.getLong(sysUserRoleMap.get("roleId_less"));
        Long roleId_lessEqual = TypeCast.getLong(sysUserRoleMap.get("roleId_lessEqual"));
        List<Long> roleId_in = TypeCast.getLongList(sysUserRoleMap.get("roleId_in"));
        List<Long> roleId_notIn = TypeCast.getLongList(sysUserRoleMap.get("roleId_notIn"));
        Long roleId_between1 = TypeCast.getLong(sysUserRoleMap.get("roleId_between1"));
        Long roleId_between2 = TypeCast.getLong(sysUserRoleMap.get("roleId_between2"));
        Long roleId_notBetween1 = TypeCast.getLong(sysUserRoleMap.get("roleId_notBetween1"));
        Long roleId_notBetween2 = TypeCast.getLong(sysUserRoleMap.get("roleId_notBetween2"));

        if (roleId_null != null) {
            c.andRoleIdIsNull();
        }
        if (roleId_notNull != null) {
            c.andRoleIdIsNotNull();
        }
        if (roleId != null) {
            c.andRoleIdEqualTo(roleId);
        }
        if (roleId_not != null) {
            c.andRoleIdNotEqualTo(roleId_not);
        }
        if (roleId_greater != null) {
            c.andRoleIdGreaterThan(roleId_greater);
        }
        if (roleId_greaterEqual != null) {
            c.andRoleIdGreaterThanOrEqualTo(roleId_greaterEqual);
        }
        if (roleId_less != null) {
            c.andRoleIdLessThan(roleId_less);
        }
        if (roleId_lessEqual != null) {
            c.andRoleIdLessThanOrEqualTo(roleId_lessEqual);
        }
        if (roleId_in != null) {
            c.andRoleIdIn(roleId_in);
        }
        if (roleId_notIn != null) {
            c.andRoleIdNotIn(roleId_notIn);
        }
        if (roleId_between1 != null) {
            c.andRoleIdBetween(roleId_between1, roleId_between2);
        }
        if (roleId_notBetween1 != null) {
            c.andRoleIdNotBetween(roleId_notBetween1, roleId_notBetween2);
        }
        if (sysUserRoleMap.get("orderBy") != null) {
            sysUserRoleEx.setOrderByClause((String) sysUserRoleMap.get("orderBy"));
        }
        if (limitStart != null && limitOffset != null) {
            sysUserRoleEx.setLimitStart(limitStart);
            sysUserRoleEx.setLimitOffset(limitOffset);
        }

        return sysUserRoleEx;
    }
}
